package com.ymatou.userservice.testcase;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.userservice.parameter.GetUserThirdPartyAccountDtoBean;
import com.ymatou.userservice.service.GetUserThirdPartyAccountDtoCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P3;
import com.ymttest.business.service.UserCallService;
import com.ymttest.database.sqlwapper.YmtUsersWapper;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.constant.ECode;
import com.ymttest.utils.constant.ThirdPartyType;

/**
*设置昵称接口
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetUserThirdPartyAccountDto{
	private static GetUserThirdPartyAccountDtoBean getuserthirdpartyaccountdtoBean;
	private static GetUserThirdPartyAccountDtoCall getuserthirdpartyaccountdtoCall;
	private static YmtUsersWapper userWapper;
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取用户第三方账号接口");
		userWapper = new YmtUsersWapper();
	}

	@Before
	public void caseUp() {
		getuserthirdpartyaccountdtoBean=new GetUserThirdPartyAccountDtoBean();
		getuserthirdpartyaccountdtoCall=new GetUserThirdPartyAccountDtoCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getuserthirdpartyaccountdtoCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetUserThirdPartyAccountDto_001() {
		Logger.start(true,"获取用户绑定的微博账号(该用户只绑定了微博账号)，成功");
		try {
			Logger.comment("从数据库获取值绑定了微博的账号");
			List<Map> users = userWapper.selectThridPartyBindYmt("Ymt_User_At_SinaWeibo", "YmatouUserId", "not in", 
					"iYmtUserId", "Ymt_User_At_Alipay", "not in", "iYmtUserId", "Ymt_User_At_Wexin_Identify");
			List<Map> thirdParty = new ArrayList<Map>();
			for(Map user : users){
				addThirdPartyAccount(thirdParty, ThirdPartyType.WEIBO, user.get("sWeiboUserId").toString(), 
						(user.get("IsBind").toString().equals("true")? true:false), formatDate(user.get("dAddTime")));
			}
			
			getuserthirdpartyaccountdtoBean.setUserId((Integer)users.get(0).get("YmatouUserId"));
			getuserthirdpartyaccountdtoCall.setData(getuserthirdpartyaccountdtoBean);
			getuserthirdpartyaccountdtoCall.callService();			
			
			Logger.verifyEquals("true", getuserthirdpartyaccountdtoCall.getString("Success"), "验证操作成功");
			Logger.verifyEquals(ECode.SUCCESS, getuserthirdpartyaccountdtoCall.getString("ResponseCode"), "验证Code");
			Logger.verifyEquals("ok", getuserthirdpartyaccountdtoCall.getString("SuccessMessage"), "验证Msg信息");
			
			Logger.verifyEquals(thirdParty.size(), getuserthirdpartyaccountdtoCall.getResult().size(), "验证返回的数据条数");
			for(int i=0; i<thirdParty.size(); i++){
				boolean flag = false;
				Map actual = getuserthirdpartyaccountdtoCall.getResult().get(i);
				for(int j=0; j<thirdParty.size(); j++){
					Map expected = thirdParty.get(j);
					if(expected.get("ThirdPartyId").toString().equals(actual.get("ThirdPartyId").toString())){
						Logger.verifyEquals(expected.get("IsBind"), actual.get("IsBind"), "验证是否绑定");
						Logger.verifyEquals(expected.get("ThirdPartyType"), actual.get("ThirdPartyType"), "验证第三方类型");
//						Logger.verifyEquals(expected.get("dAddTime") , actual.get("CreateTime"), "验证创建时间");
						flag=true;
					}
				}
				if(!flag){
					Logger.fail(new Exception("第三方"+actual+"-与实际绑定情况不符"));
				}
			}
			
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetUserThirdPartyAccountDto_002() {
		Logger.start(true,"获取用户绑定的微信账号，成功");
		try {
			Logger.comment("从数据库获取只绑定了微信的账号");
			List<Map> users = userWapper.selectThridPartyBindYmt("Ymt_User_At_Wexin_Identify", "iYmtUserId", "not in", 
					"iYmtUserId", "Ymt_User_At_Alipay", "not in", "YmatouUserId", "Ymt_User_At_SinaWeibo");
			List<Map> thirdParty = new ArrayList<Map>();
			for(Map user : users){
				addThirdPartyAccount(thirdParty, ThirdPartyType.WEIXIN, user.get("sUnionId").toString(), 
						(user.get("IsBind").toString().equals("true")? true:false), formatDate(user.get("dAddTime")));
			}
			getuserthirdpartyaccountdtoBean.setUserId((Integer)users.get(0).get("iYmtUserId"));
			getuserthirdpartyaccountdtoCall.setData(getuserthirdpartyaccountdtoBean);
			getuserthirdpartyaccountdtoCall.callService();			
			
			Logger.verifyEquals("true", getuserthirdpartyaccountdtoCall.getString("Success"), "验证操作成功");
			Logger.verifyEquals(ECode.SUCCESS, getuserthirdpartyaccountdtoCall.getString("ResponseCode"), "验证Code");
			Logger.verifyEquals("ok", getuserthirdpartyaccountdtoCall.getString("SuccessMessage"), "验证Msg信息");
			
			Logger.verifyEquals(thirdParty.size(), getuserthirdpartyaccountdtoCall.getResult().size(), "验证返回的数据条数");
			for(int i=0; i<thirdParty.size(); i++){
				boolean flag = false;
				Map actual = getuserthirdpartyaccountdtoCall.getResult().get(i);
				for(int j=0; j<thirdParty.size(); j++){
					Map expected = thirdParty.get(j);
					if(expected.get("ThirdPartyId").toString().equals(actual.get("ThirdPartyId").toString())){
						Logger.verifyEquals(expected.get("IsBind"), actual.get("IsBind"), "验证是否绑定");
						Logger.verifyEquals(expected.get("ThirdPartyType"), actual.get("ThirdPartyType"), "验证第三方类型");
//						Logger.verifyEquals(expected.get("dAddTime") , actual.get("CreateTime"), "验证创建时间");
						flag=true;
					}
				}
				if(!flag){
					Logger.fail(new Exception("第三方"+actual+"-与实际绑定情况不符"));
				}
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetUserThirdPartyAccountDto_003() {
		Logger.start(true,"获取用户绑定的阿里账号，成功");
		try {
			Logger.comment("从数据库获取只绑定了阿里的账号");
			List<Map> users = userWapper.selectThridPartyBindYmt("Ymt_User_At_Alipay", "iYmtUserId", "not in", 
					"YmatouUserId", "Ymt_User_At_SinaWeibo", "not in", "iYmtUserId", "Ymt_User_At_Wexin_Identify");
			List<Map> thirdParty = new ArrayList<Map>();
			for(Map user : users){
				addThirdPartyAccount(thirdParty, ThirdPartyType.ALI, user.get("sUserId").toString(), 
						(user.get("IsBind").toString().equals("true")? true:false), formatDate(user.get("dAddTime")));
			}
			
			getuserthirdpartyaccountdtoBean.setUserId((Integer)users.get(0).get("iYmtUserId"));
			getuserthirdpartyaccountdtoCall.setData(getuserthirdpartyaccountdtoBean);
			getuserthirdpartyaccountdtoCall.callService();			
			
			Logger.verifyEquals("true", getuserthirdpartyaccountdtoCall.getString("Success"), "验证操作成功");
			Logger.verifyEquals(ECode.SUCCESS, getuserthirdpartyaccountdtoCall.getString("ResponseCode"), "验证Code");
			Logger.verifyEquals("ok", getuserthirdpartyaccountdtoCall.getString("SuccessMessage"), "验证Msg信息");
			
			Logger.verifyEquals(thirdParty.size(), getuserthirdpartyaccountdtoCall.getResult().size(), "验证返回的数据条数");
			for(int i=0; i<thirdParty.size(); i++){
				boolean flag = false;
				Map actual = getuserthirdpartyaccountdtoCall.getResult().get(i);
				for(int j=0; j<thirdParty.size(); j++){
					Map expected = thirdParty.get(j);
					if(expected.get("ThirdPartyId").toString().equals(actual.get("ThirdPartyId").toString())){
						Logger.verifyEquals(expected.get("IsBind"), actual.get("IsBind"), "验证是否绑定");
						Logger.verifyEquals(expected.get("ThirdPartyType"), actual.get("ThirdPartyType"), "验证第三方类型");
//						Logger.verifyEquals(expected.get("dAddTime") , actual.get("CreateTime"), "验证创建时间");
						flag=true;
					}
				}
				if(!flag){
					Logger.fail(new Exception("第三方"+actual+"-与实际绑定情况不符"));
				}
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetUserThirdPartyAccountDto_004() {
		Logger.start(true,"获取用户绑定的多个三方账号，成功");
		try {
			String phone = TestDataManager.getPhoneNum();
			Map usermap = UserCallService.regByAli("regAli_"+TestDataManager.getRandomNum(7), phone, true, "注册成功");
			Thread.sleep(3000);
			UserCallService.regByWeibo("bindWeibo_"+TestDataManager.getRandomNum(7), phone, true, "登录成功");
			Thread.sleep(3000);
			UserCallService.regByWeiXin("binWeixin_"+TestDataManager.getRandomNum(7), phone, true, "登录成功");
			Thread.sleep(3000);
			int userId = ((Double)usermap.get("userId")).intValue();
			
			getuserthirdpartyaccountdtoBean.setUserId(userId);
			getuserthirdpartyaccountdtoCall.setData(getuserthirdpartyaccountdtoBean);
			getuserthirdpartyaccountdtoCall.callService();			
			
			Logger.verifyEquals("true", getuserthirdpartyaccountdtoCall.getString("Success"), "验证操作成功");
			Logger.verifyEquals(ECode.SUCCESS, getuserthirdpartyaccountdtoCall.getString("ResponseCode"), "验证Code");
			Logger.verifyEquals("ok", getuserthirdpartyaccountdtoCall.getString("SuccessMessage"), "验证Msg信息");
			
			Logger.verifyEquals(3, getuserthirdpartyaccountdtoCall.getResult().size(), "验证只有一条数据返回");
			
			List<Map<String, Object>> list=getuserthirdpartyaccountdtoCall.getResult();
			Map<String, Map> result=new HashMap<String, Map>();
			for (Map<String, Object> map : list) {
				result.put(map.get("ThirdPartyId").toString(), map);
			}
			Logger.comment("验证返回的阿里账号绑定的情况");
			Map aliUser = userWapper.selectThirdUserByYmtUserId("Ymt_User_At_Alipay", "iYmtUserId", userId);
			Map result1 = result.get(aliUser.get("sUserId"));
			Logger.verifyEquals(aliUser.get("IsBind"), result1.get("IsBind"), "验证是否绑定");
			Logger.verifyEquals(aliUser.get("sUserId"), result1.get("ThirdPartyId"), "验证第三方Id");
			Logger.verifyEquals("Alipay", result1.get("ThirdPartyType"), "验证第三方类型");
//			Logger.verifyEquals(formatDate(aliUser.get("dAddTime")) , result1.get("CreateTime"), "验证创建时间");
			
			
			Logger.comment("验证返回的微信账号绑定的情况");
			Map weiXinUser = userWapper.selectThirdUserByYmtUserId("Ymt_User_At_Wexin_Identify", "iYmtUserId", userId);
			Map result2 = result.get(weiXinUser.get("sUnionId"));
			Logger.verifyEquals(weiXinUser.get("IsBind"), result2.get("IsBind"), "验证是否绑定");
			Logger.verifyEquals(weiXinUser.get("sUnionId"), result2.get("ThirdPartyId"), "验证第三方Id");
			Logger.verifyEquals("WeiXin", result2.get("ThirdPartyType"), "验证第三方类型");
//			Logger.verifyEquals(formatDate(weiXinUser.get("dAddTime")) , result2.get("CreateTime"), "验证创建时间");
			
			Logger.comment("验证返回的微博账号绑定的情况");
			Map weiboUser = userWapper.selectThirdUserByYmtUserId("Ymt_User_At_SinaWeibo", "YmatouUserId", userId);
			Map result3 = result.get(weiboUser.get("sWeiboUserId"));
			Logger.verifyEquals(weiboUser.get("IsBind"), result3.get("IsBind"), "验证是否绑定");
			Logger.verifyEquals(weiboUser.get("sWeiboUserId"), result3.get("ThirdPartyId"), "验证第三方Id");
			Logger.verifyEquals("Weibo", result3.get("ThirdPartyType"), "验证第三方类型");
//			Logger.verifyEquals(formatDate(weiboUser.get("dAddTime")) , result3.get("CreateTime"), "验证创建时间");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetUserThirdPartyAccountDto_005() {
		Logger.start(true,"用户未绑定第三方账号，获取数据为空");
		try {
			Map<String, Comparable> user = UserCallService.registerByMobile();
			getuserthirdpartyaccountdtoBean.setUserId((Integer)user.get("userid"));
			getuserthirdpartyaccountdtoCall.setData(getuserthirdpartyaccountdtoBean);
			getuserthirdpartyaccountdtoCall.callService();			
			Logger.verifyEquals("true", getuserthirdpartyaccountdtoCall.getString("Success"), "验证操作成功");
			Logger.verifyEquals(ECode.SUCCESS, getuserthirdpartyaccountdtoCall.getString("ResponseCode"), "验证Code");
			Logger.verifyEquals("ok", getuserthirdpartyaccountdtoCall.getString("SuccessMessage"), "验证Msg信息");
			
			Logger.verifyEquals(0, getuserthirdpartyaccountdtoCall.getResult().size(), "验证没有数据返回");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P3.class)
	@TestCase
	public void Tc_GetUserThirdPartyAccountDto_006() {
		Logger.start(false,"未输入用户Id，失败");
		try {
			getuserthirdpartyaccountdtoCall.setData(getuserthirdpartyaccountdtoBean);
			getuserthirdpartyaccountdtoCall.callService();			
			Logger.verifyEquals("false", getuserthirdpartyaccountdtoCall.getString("Success"), "验证操作失败");
			Logger.verifyEquals(ECode.ERROR400, getuserthirdpartyaccountdtoCall.getString("ResponseCode"), "验证Code");
			Logger.verifyEquals("错误的请求参数", getuserthirdpartyaccountdtoCall.getString("LastErrorMessage"), "验证Msg信息");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	/**
	 * 格式化时间，目前接口返回的时间形式为："CreateTime":"\/Date(1460603463543-0000)\/"
	 * @param dateStr
	 * @return
	 */
	private String formatDate(Object dateStr){
		return "/Date("+((Date)dateStr).getTime()+"-0000)/";
	}
	
	/**
	 * 添加需要验证的第三方账号
	 * @param thirdPartyType
	 * @param thirdPartyId
	 * @param isBind
	 */
	private void addThirdPartyAccount(List<Map> thirdParty, String thirdPartyType, String thirdPartyId,
			boolean isBind, String addTime){
		Map thirdTypeAccount = new HashMap<String, Object>();
		thirdTypeAccount.put("ThirdPartyType", thirdPartyType);
		thirdTypeAccount.put("ThirdPartyId", thirdPartyId);
		thirdTypeAccount.put("IsBind", isBind);
		thirdTypeAccount.put("dAddTime", addTime);
		thirdParty.add(thirdTypeAccount);
	}
}